-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[cookbook] fixed file uploads with Doctrine samples to upload the file wh #564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The ``@ORM\PrePersist()`` and ``@ORM\PostPersist()`` event callbacks are | ||
triggered before and after the entity is persisted to the database. On the | ||
other hand, the ``@ORM\PreUpdate()`` and ``@ORM\PostUpdate()`` event | ||
callbacks are called when the entity is udpated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably meant "updated" :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks fixed.
Hey Hugo! Ok, so you're definitely correct with this PR, but to be a truly functional system, should we also include something that would handle unlinking the old file when a new file is updated? I believe with this, each time you "update" the record by uploading a new file, the old file will still remain on the filesystem. Thoughts? |
…e when the object is both persisted and updated. Added a note about PrePersist, PreUpdate, PostPersist and PostUpdate lifecycle callback events.
Hi Ryan, I'm ready with this PR. I have fixed it and tested it while I was giving a training. We were trying the file uploads with Doctrine and we noticed the update was not proceeded because of the missing @PreUpdate and @postupdate lifecycle callbacks. You can merge the PR and then I will open a new PR to remove the old file on the filesystem in case of the entity is updated with a new file upload with a different name. Cheers. |
In fact, don't merge it now... I need to recheck the code. |
So I rechecked and the code is good but there is a "bug" in Doctrine2. The pre and post update callbacks are only called if at least one property of the object has changed. If we try to only change the uploaded file without changing the other properties, the callbacks are not called... and that sucks... I will add a note in the PR. |
it is not a bug but a feature. The update event is dispatched only if the entity is updated, not for all entities that were queried during the request. |
Yes it's a feature and a quite logic behavior. Nevertheless, if we want to trigger the callbacks we have to use a hack. For example, we can rely on an updatedAt field in the entity that is changed everytime the file is uploaded.
But it's still a hack and it's probably not the best solution to consider the lifecycle callbacks when dealing with file uploads and doctrine entities... |
[cookbook] fixed file uploads with Doctrine samples to upload the file wh
Hi Hugo! Thanks for the good research, I understand the problem. I've merged in this PR - the entry is obviously not perfect yet, but it's better. I've added a caution message temporarily: 808da21 Perhaps this is a conversation for the dev-board. What we need is an agreed-upon "best practice" for this situation. So far, the example in this entry really doesn't fully solve the problem. Thanks! |
Also see #600, so we can keep this on our radar. |
Great! Thanks Ryan ;) |
I think that another solution that may be included in the documentation is to update the already existing path field instead of using "updatedAt":
|
I'm having a problem with the following situation: On the edit screen of a entity that has OneToOne association to documents, if I click save without adding an image a register is created at the database on document, but path stays null, because i did not selected the image. What I want is dont create any entry on the 'document' table. Is there a way to do this? |
[cookbook] fixed file uploads with Doctrine samples to upload the file when the object is both persisted and updated. Added a note about PrePersist, PreUpdate, PostPersist and PostUpdate lifecycle callback events.